import { defineStore } from 'pinia'
import { getHomeData, getHomeMultidata } from '../../service/home/home.js'

// 模拟从接口或者其他地方获取配置选项卡组件配置
export const types = ['pop', 'new', 'sell']

function getDefaultGoodsListData() {
  let goodsListOrigin = {}
  types.forEach((type) => {
    goodsListOrigin[type] = {
      page: 0,
      list: []
    }
  })
	
  return goodsListOrigin
}

export const useHomeStore = defineStore('home', {
  state: () => {
    return {
      banners: [],
      recommends: [],
      goodsList: getDefaultGoodsListData() // 动态生成数据结构
    }
  },
  actions: {
    // 获取首页轮播图和推荐栏的数据
    fetchHomeMultidata() {
      getHomeMultidata().then(res => {
        this.banners = res.data.banner.list || []
        this.recommends = res.data.recommend.list || []
      })
    },
    fetchHomeData(type, page) {
      getHomeData(type, page).then(res => {
        this.goodsList[type].list.push(...res.data.list)
        this.goodsList[type].page = page
      })
    }
  }
})